
export class BitSet<T = number> {
    private map = new Map<T, boolean>();
    private trueCount = 0;

    get size(): number {
        return this.map.size;
    }

    set(channel: T, val: boolean): void {
        const prev = this.map.get(channel);
        if (prev !== val) {
            if (val) this.trueCount++;
            else if (prev === true) this.trueCount--;
        }
        this.map.set(channel, val);
    }

    get(channel: T): boolean | undefined {
        return this.map.get(channel);
    }

    one(val: boolean): boolean {
        return val ? this.trueCount > 0 : this.trueCount < this.size;
    }

    all(val: boolean): boolean {
        if (this.size === 0) return false;
        return val ? this.trueCount === this.size : this.trueCount === 0;
    }

    print(): void {
        for (const [key, value] of this.map) {
            console.log(`${String(key)}: ${value}`);
        }
    }
    
}